#!/bin/sh

# SetupWiFi.sh
#
# SetupWireless is released under the General Public License, (C) 2008 Jeff Dyck
#
# This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

# This script will accept arguments for -u username -p password and -w wireless network
# and will attempt to configure the appropriate 3 plists to autoconfigure 802.1x wifi
# access.

# Quickly define proper usage of this script
usage="SetupWifi.sh [-u 802.1x username] [-p 802.1x password] [-w wireless network name]"

# Parse the options...
while getopts "u:p:w: h" options
do
	case $options in
		h ) echo $usage
			exit 1;;
		u ) USER=$OPTARG;;
		p ) PASS=$OPTARG;;
		w ) WIRELESS=$OPTARG;;
		* ) usage
			exit 1;;			
	esac
done

# Deal with missing flags...
if [[ -z $USER ]] || [[ -z $PASS ]] || [[ -z $WIRELESS ]]
then
	echo ""
	echo "You seem to be missing a flag for this program to work properly."
    echo $usage
    exit 1
fi

# Define some handy variables...

Buddy="/usr/libexec/PlistBuddy"
LocalUser=`whoami`
HOME=`dscl . -read /Users/$LocalUser home | sed -e 's|dsAttrTypeNative:home: ||g'`
LOG=$HOME/Library/Logs/SetupWifi.log


# Get MAC Addresses
hwAddress=`ifconfig en0 | awk '/ether/ { gsub(":", ""); print $2 }'`
hwAddresswithColons=`ifconfig en0 | awk '/ether/ { gsub(":", "\\\\:"); print $2 }'`

AirAddress=`ifconfig en1 | awk '/ether/ { gsub(":", ""); print $2 }'`
AirAddresswithColons=`ifconfig en1 | awk '/ether/ { gsub(":", "\\\\:"); print $2 }'`
AirAddresswithColons2=`ifconfig en1 | awk '/ether/ { gsub(":", "\\:"); print $2 }'`


# Get some Unique Identifiers to use in the plists...
uuid=`uuidgen`
netuuid=`uuidgen`

# DYNAMICALLY SET THE LEOPARD UUID FOR THE BYHOST FILE NAMING
if [[ `ioreg -rd1 -c IOPlatformExpertDevice | grep -i "UUID" | cut -c27-50` == "00000000-0000-1000-8000-" ]]; then
	LEOUUID=`ioreg -rd1 -c IOPlatformExpertDevice | grep -i "UUID" | cut -c51-62 | awk {'print tolower()'}`
elif [[ `ioreg -rd1 -c IOPlatformExpertDevice | grep -i "UUID" | cut -c27-50` != "00000000-0000-1000-8000-" ]]; then
	LEOUUID=`ioreg -rd1 -c IOPlatformExpertDevice | grep -i "UUID" | cut -c27-62`
fi


# Define the 3 plist files for easier reference...
AirportPref=/Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist
EAPProfiles="$HOME/Library/Preferences/com.apple.eap.profiles.plist"
EAPBindings="$HOME/Library/Preferences/ByHost/com.apple.eap.bindings.$LEOUUID.plist"

#These were for testing purposes while building the plistbuddy statements...
#AirportPref=$HOME/Desktop/com.apple.airport.preferences.plist
#EAPProfiles=$HOME/Desktop/com.apple.eap.profiles.plist
#EAPBindings=$HOME/Desktop/com.apple.eap.bindings.$hwAddress.plist

# Setup the log so we know what's going on later...

echo "" >> $LOG
echo "Creating some plist files to allow the Wireless to auto-connect..." >> $LOG
echo "------------------------------------------------------------------" >> $LOG
echo "" >> $LOG
echo "User to configure: $USER" >> $LOG
echo "802.1x network to auto-connect to: $WIRELESS" >> $LOG
echo "Computer Hardware address: $hwAddress" >> $LOG


# First we need to build the System AirportPref...

"$Buddy" -c "Delete :KnownNetworks" $AirportPref 	;# Remove any previous entry.

"$Buddy" -c "Add :KnownNetworks dict" $AirportPref
"$Buddy" -c "Add :KnownNetworks:$uuid dict" $AirportPref
"$Buddy" -c "Add :KnownNetworks:$uuid:Remembered\ Channels array" $AirportPref
"$Buddy" -c "Add :KnownNetworks:$uuid:Remembered\ Channels:0 integer 1" $AirportPref
"$Buddy" -c "Add :KnownNetworks:$uuid:Remembered\ Channels:1 integer 8" $AirportPref
"$Buddy" -c "Add :KnownNetworks:$uuid:SCAN_DIRECTED bool Yes" $AirportPref
"$Buddy" -c "Add :KnownNetworks:$uuid:SecurityType string 802.1X\ WEP" $AirportPref
"$Buddy" -c "Add :KnownNetworks:$uuid:SSID_STR string $WIRELESS" $AirportPref
"$Buddy" -c "Add :Version integer 6" $AirportPref


# Building the User's EAPProfiles settings...

"$Buddy" -c "Delete :Profiles" $EAPProfiles 	;# Remove any previous entry.

"$Buddy" -c "Add :Profiles array" $EAPProfiles >> $LOG
"$Buddy" -c "Add :Profiles:0 dict" $EAPProfiles >> $LOG
"$Buddy" -c "Add :Profiles:0:ConnectByDefault bool Yes" $EAPProfiles >> $LOG
"$Buddy" -c "Add :Profiles:0:EAPClientConfiguration dict" $EAPProfiles >> $LOG
"$Buddy" -c "Add :Profiles:0:EAPClientConfiguration:AcceptEAPTypes array" $EAPProfiles >> $LOG
"$Buddy" -c "Add :Profiles:0:EAPClientConfiguration:AcceptEAPTypes:0 integer 21" $EAPProfiles >> $LOG
"$Buddy" -c "Add :Profiles:0:EAPClientConfiguration:AcceptEAPTypes:1 integer 25" $EAPProfiles >> $LOG
"$Buddy" -c "Add :Profiles:0:EAPClientConfiguration:UserName string $USER" $EAPProfiles >> $LOG
"$Buddy" -c "Add :Profiles:0:EAPClientConfiguration:UserPasswordKeychainItemID string $uuid" $EAPProfiles >> $LOG
"$Buddy" -c "Add :Profiles:0:UniqueIdentifier string $uuid" $EAPProfiles >> $LOG
"$Buddy" -c "Add :Profiles:0:UserDefinedName string $USER" $EAPProfiles >> $LOG
"$Buddy" -c "Add :Profiles:0:Wireless\ Network string $WIRELESS" $EAPProfiles >> $LOG
"$Buddy" -c "Add :Profiles:0:userDefinedName string $USER-WPA" $EAPProfiles >> $LOG


# Building the User's EAPBindings settings...

"$Buddy" -c "Delete :$AirAddresswithColons" $EAPBindings 	;# Remove any previous entry.

"$Buddy" -c "Add :$AirAddresswithColons array" $EAPBindings >> $LOG
"$Buddy" -c "Add :$AirAddresswithColons:0 dict" $EAPBindings >> $LOG
"$Buddy" -c "Add :$AirAddresswithColons:0:Hardware\ Address string $AirAddresswithColons2" $EAPBindings >> $LOG
"$Buddy" -c "Add :$AirAddresswithColons:0:UniqueIdentifier string $uuid" $EAPBindings >> $LOG
"$Buddy" -c "Add :$AirAddresswithColons:0:Wireless\ Network string $WIRELESS" $EAPBindings >> $LOG


# Now we need to add a password to the keychain with the uuid 
security add-generic-password -a $USER -s $uuid -p $PASS >> $LOG

# If I knew how to edit the access settings of the keychain entry I just made I would allow eapservice to access it.
# As it stands now, the user needs to either:
#
# 1: Manually choose the wireless network from the Airport menu, or
# 2: Turn off airport then turn it back on again, or
# 3: Restart.
#
# In any case, they will get a dialog box asking them if they want eapservice to have access to the keychain entry
# identified by the uuid generated above.

exit 0